package com.loja.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class GenericDAO<T> {
	

	private Session sessao;
      
	private Class<T> classe;  
    
  
	public GenericDAO(Session sessao) {
  		this.sessao=sessao; 
	    sessao.beginTransaction();
  	}
    
    private void save(T u) {  
        this.sessao.save(u);  
    }  
  
    public void commit() {  
    	sessao.getTransaction().commit();  
   
    }  
  
  
  
    public void rollback() {  
    	sessao.getTransaction().rollback();    
    }  
  
    public void close() {  
        this.sessao.close();  
    }  
  
    public void adiciona(T u) {  
        save(u);  
 
    }  
  
    public void remove(T u) {  
        this.sessao.delete(u);  
    }  
  
    public void atualiza(T u) {  
        this.sessao.merge(u);  
    }  
  
    @SuppressWarnings("unchecked")  
    public List<T> listaTudo() {  
        return this.sessao.createCriteria(this.classe).list();  
    }  
    
    @SuppressWarnings("unchecked")
	public T procura(Long id) {  
        return (T) sessao.load(this.classe,id);  
        }


	public Class<T> getClasse() {
		return  classe;
	}

	public void setClasse(Class<T> classe) {
		this.classe = classe;
	}
}
